package com.guonl.controller.gen;

import com.guonl.controller.BaseController;
import com.guonl.entity.gen.GenDadabase;
import com.guonl.model.FrontResult;
import com.guonl.model.form.TableListForm;
import com.guonl.model.page.TableDataInfo;
import com.guonl.service.gen.IGenDadabaseService;
import com.guonl.utils.ExcelUtil;
import com.guonl.vo.TableListQueryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 数据库配置Controller
 *
 * @author guonl
 * @date 2021-04-01
 */
@Controller
@RequestMapping("/gen/db")
public class GenDadabaseController extends BaseController {

    @Autowired
    private IGenDadabaseService genDadabaseService;

    @GetMapping()
    public String dadabase() {
        return "gen/dadabase";
    }

    /**
     * 查询数据库配置列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GenDadabase genDadabase) {
        startPage();
        List<GenDadabase> list = genDadabaseService.selectGenDadabaseList(genDadabase);
        return getDataTable(list);
    }

    /**
     * 导出数据库配置列表
     */
    @PostMapping("/export")
    @ResponseBody
    public FrontResult export(GenDadabase genDadabase) {
        List<GenDadabase> list = genDadabaseService.selectGenDadabaseList(genDadabase);
        ExcelUtil<GenDadabase> util = new ExcelUtil<GenDadabase>(GenDadabase.class);
        return util.exportExcel(list, "dadabase");
    }

    /**
     * 新增数据库配置
     */
    @GetMapping("/add")
    public String add() {
        return "gen/add";
    }

    /**
     * 新增保存数据库配置
     */
    @PostMapping("/add")
    @ResponseBody
    public FrontResult addSave(GenDadabase genDadabase) {
        return FrontResult.success(genDadabaseService.insertGenDadabase(genDadabase));
    }

    /**
     * 修改数据库配置
     */
    @GetMapping("/edit/{dbId}")
    public String edit(@PathVariable("dbId") Long dbId, ModelMap mmap) {
        GenDadabase genDadabase = genDadabaseService.selectGenDadabaseById(dbId);
        mmap.put("genDadabase", genDadabase);
        return "gen/edit";
    }

    /**
     * 修改保存数据库配置
     */
    @PostMapping("/edit")
    @ResponseBody
    public FrontResult editSave(GenDadabase genDadabase) {
        return FrontResult.success(genDadabaseService.updateGenDadabase(genDadabase));
    }

    /**
     * 删除数据库配置
     */
    @PostMapping("/remove")
    @ResponseBody
    public FrontResult remove(String ids) {
        return FrontResult.success(genDadabaseService.deleteGenDadabaseByIds(ids));
    }

    /**
     * 列表详情
     * @param dbId
     * @param mmap
     * @return
     */
    @GetMapping("/tableList/{dbId}")
    public String tableList(@PathVariable("dbId") Long dbId, ModelMap mmap) {
        mmap.put("dbId",dbId);
        return "gen/tableList";
    }

    @ResponseBody
    @PostMapping("/tableList")
    public TableDataInfo tableList(TableListQueryVO queryVO) {
//        startPage();
        List<TableListForm> list = genDadabaseService.selectDbTableList(queryVO);
        return getDataTable(list);
    }

}
